package com.qyhl.webtv.module_news.news.newslist;


import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.tools.ScreenUtils;
import com.orhanobut.hawk.Hawk;
import com.qinanyu.bannerview.holder.SimpleHolder;
import com.qinanyu.bannerview.holder.SimpleHolderCreator;
import com.qinanyu.bannerview.listener.OnItemClickListener;
import com.qinanyu.bannerview.view.SimpleBannerView;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;
import com.qyhl.webtv.commonlib.entity.news.AdvertiseBean;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.entity.news.NewsStyleBean;
import com.qyhl.webtv.commonlib.item.ItemAdvCommon;
import com.qyhl.webtv.commonlib.item.ItemAdvGroup;
import com.qyhl.webtv.commonlib.item.ItemAdvLarge;
import com.qyhl.webtv.commonlib.item.ItemCatchNews;
import com.qyhl.webtv.commonlib.item.ItemCommonLarge;
import com.qyhl.webtv.commonlib.item.ItemCommonLeft;
import com.qyhl.webtv.commonlib.item.ItemCommonRight;
import com.qyhl.webtv.commonlib.item.ItemCommonThreePics;
import com.qyhl.webtv.commonlib.item.ItemGoodLife;
import com.qyhl.webtv.commonlib.item.ItemNoPicture;
import com.qyhl.webtv.commonlib.item.ItemOtherAdv;
import com.qyhl.webtv.commonlib.item.ItemPicture;
import com.qyhl.webtv.commonlib.item.ItemSmallVideo;
import com.qyhl.webtv.commonlib.item.ItemTitleNews;
import com.qyhl.webtv.commonlib.item.ItemTopNews;
import com.qyhl.webtv.commonlib.item.ItemTopNoPicture;
import com.qyhl.webtv.commonlib.item.ItemUnion;
import com.qyhl.webtv.commonlib.item.ItemVideoLarge;
import com.qyhl.webtv.commonlib.item.ItemVideoLeft;
import com.qyhl.webtv.commonlib.item.ItemVideoRight;
import com.qyhl.webtv.commonlib.utils.ItemSkipUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * A simple {@link Fragment} subclass.
 * 新闻列表
 */
public class NewListFragment extends BaseFragment implements NewsListContract.NewsListView {
    SimpleBannerView mNewsListBanner;
    @BindView(R2.id.listview)
    RecyclerView listview;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private String sectionId; //新闻栏目ID
    private View view;


    private boolean hasPicture;//是否是无图

    private List<GlobalNewsBean> mDataList;
    private List<GlobalNewsBean> indexList;
    private List<AdvHomeBean> advList;//广告数据

    private List<NewsBean> bannerList;//置顶数据
    private List<NewsBean> newsList;//新闻数据

    private int topNum = 0;//置顶数


    private String newsId = "0";//新闻最后一条id,用于请求更多数据的参数

    private int page = 1;//请求页数,当sectionId大于100W为页数

    private RequestOptions options;

    private NewsListPresenter mPresenter;

    private int[] pos;//改变样式的位置

    private int position;

    private int advPostion = 0;//广告位置
    private HeaderAndFooterWrapper mHeaderAndFooterWrapper;
    private String url;

    private boolean isFragmentShow = false;


    public static NewListFragment newInstance(String sectionId) {
        NewListFragment fragment = new NewListFragment();
        fragment.setSectionId(sectionId);
        return fragment;
    }

    @Override
    public void initImmersionBar() {
    }

    private void setSectionId(String url) {
        sectionId = url;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.news_fragment_new_list, null);
    }

    @Override
    protected void create() {
        mPresenter = new NewsListPresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListenters();
        url = CommonUtils.getInstance().getCmsUrl();
        if (StringUtils.isNotEmpty(sectionId)) {
            mPresenter.getHeaderNum(sectionId, url);
        } else {
            setEmpty("暂无任何内容！");
        }
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }



    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mDataList = new ArrayList<>();
        advList = new ArrayList<>();
        bannerList = new ArrayList<>();
        newsList = new ArrayList<>();


        //设置刷新控件
        refresh.setRefreshHeader(new MaterialHeader(getActivity()));
        refresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        refresh.setEnableLoadMore(true);
        refresh.setDisableContentWhenLoading(true);
        refresh.setDisableContentWhenRefresh(true);

        //glide 配置
        options = new RequestOptions()
                .placeholder(R.drawable.cover_large_default)
                .error(R.drawable.cover_large_default)
                .priority(Priority.HIGH);

        //获取样式
        List<NewsStyleBean> styleBeanList = CommonUtils.getInstance().getGlobalStyleBeanList();
        boolean globalStyle = CommonUtils.getInstance().getGlobalStyle();
        if (globalStyle) {
            if (styleBeanList.size() > 0) {
                pos = new int[styleBeanList.size() - 1];
                for (int i = 1; i < styleBeanList.size(); i++) {
                    if (styleBeanList.get(i).getPosition() != 0) {
                        pos[i - 1] = styleBeanList.get(i).getPosition();
                    }
                }
            } else {

            }
        } else {
            if (styleBeanList != null && styleBeanList.size() > 0) {
                pos = new int[styleBeanList.size()];
                for (int i = 0; i < styleBeanList.size(); i++) {
                    pos[i] = styleBeanList.get(i).getPosition();
                }
            }
        }


        View headView = LayoutInflater.from(getContext()).inflate(R.layout.news_header_infomation_layout, null);
        mNewsListBanner = (SimpleBannerView) headView.findViewById(R.id.infomation_banner);
        listview.setLayoutManager(new LinearLayoutManager(getContext()));
        listview.addItemDecoration(new com.luck.picture.lib.decoration.RecycleViewDivider(
                getContext(), LinearLayoutManager.HORIZONTAL, 1, ContextCompat.getColor(getContext(), R.color.global_gray_lv4)));
        //初始化适配器
        MultiItemTypeAdapter<GlobalNewsBean> mAdapter = new MultiItemTypeAdapter<GlobalNewsBean>(getActivity(), mDataList);
        mAdapter.addItemViewDelegate(new ItemAdvCommon(getActivity()));
        mAdapter.addItemViewDelegate(new ItemAdvGroup(getActivity()));
        mAdapter.addItemViewDelegate(new ItemAdvLarge(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCatchNews(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCommonLarge(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCommonRight(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCommonLeft(getActivity()));
        mAdapter.addItemViewDelegate(new ItemGoodLife(getActivity()));
        mAdapter.addItemViewDelegate(new ItemNoPicture(getActivity()));
        mAdapter.addItemViewDelegate(new ItemPicture(getActivity()));
        mAdapter.addItemViewDelegate(new ItemTitleNews(getActivity()));
        mAdapter.addItemViewDelegate(new ItemTopNews(getActivity()));
        mAdapter.addItemViewDelegate(new ItemTopNoPicture(getActivity()));
        mAdapter.addItemViewDelegate(new ItemVideoLarge(getActivity()));
        mAdapter.addItemViewDelegate(new ItemVideoLeft(getActivity()));
        mAdapter.addItemViewDelegate(new ItemVideoRight(getActivity()));
        mAdapter.addItemViewDelegate(new ItemSmallVideo(getActivity()));
        mAdapter.addItemViewDelegate(new ItemUnion(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCommonThreePics(getActivity()));
        mAdapter.addItemViewDelegate(new ItemCommonThreePics(getContext()));
        mAdapter.addItemViewDelegate(new ItemOtherAdv(getActivity()));
        mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);
        mHeaderAndFooterWrapper.addHeaderView(headView);
        listview.setAdapter(mHeaderAndFooterWrapper);
        mHeaderAndFooterWrapper.notifyDataSetChanged();


        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                GlobalNewsBean bean = null;
                if (listview.getAdapter().getItemCount() == mDataList.size()) {
                    bean = mDataList.get(position);
                    ItemSkipUtils.getInstance().itemSkip(bean, getContext(), view);
                } else {
                    if (position != 0) {
                        bean = mDataList.get(position - 1);

                    }
                }
                ItemSkipUtils.getInstance().itemSkip(bean, getContext(), view);
                if (bean != null) {
                    if (CommonUtils.getInstance().getHasHistory()) {
                        //设置已读，保存记录于本地
                        Map<String, String> historyMap;
                        if (Hawk.contains("readHistory")) {
                            historyMap = CommonUtils.getInstance().getReadHistory();
                        } else {
                            historyMap = new HashMap<>();
                        }
                        historyMap.put(bean.getNewsId(), DateUtils.getCurrentTime());
                        CommonUtils.getInstance().putConfigData(AppConfigConstant.READ_HISTORY, historyMap);
                        Hawk.put("readHistory", historyMap);
                        mHeaderAndFooterWrapper.notifyItemChanged(position);
                    }
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    /**
     * 监听
     */

    private void setListenters() {
        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                newsId = "0";
                advPostion = 0;
                mPresenter.getHeaderNum(sectionId, url);
            }
        });

        //加载
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                advPostion = 0;
                if (Integer.parseInt(sectionId) >= 1000000) {
                    mPresenter.getDatas(sectionId, page + "");
                } else {
                    mPresenter.getDatas(sectionId, newsId);
                }
            }
        });

        //重新加载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                newsId = "0";
                advPostion = 0;
                mPresenter.getHeaderNum(sectionId, url);
            }
        });

        //轮播点击
        mNewsListBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                NewsBean bean = bannerList.get(position);
                hasPicture = StringUtils.isPictureSuccess(bean.getLogo());
                String type = "", pOne = "", pTwo = "", pThree = "";
                int style = 0;
                switch (bean.getType()) {
                    case "1"://普通新闻
                        type = "1";
                        switch (bean.getPluralPicsFlag()) {
                            case 0:
                                style = bean.getCommonStyle();
                                break;
                            case 1://三图
                                hasPicture = false;
                                if (bean.getImagess().size() == 1) {
                                    hasPicture = true;
                                    pOne = bean.getImagess().get(0).getImageUrlString();
                                } else if (bean.getImagess().size() == 2) {
                                    pOne = bean.getImagess().get(0).getImageUrlString();
                                    pTwo = bean.getImagess().get(1).getImageUrlString();
                                    hasPicture = true;
                                } else if (bean.getImagess().size() >= 3) {
                                    pOne = bean.getImagess().get(0).getImageUrlString();
                                    pTwo = bean.getImagess().get(1).getImageUrlString();
                                    pThree = bean.getImagess().get(2).getImageUrlString();
                                    hasPicture = true;
                                } else {
                                    hasPicture = false;
                                }
                                style = 4;
                                break;
                            case 2:
                                style = 3;
                                break;
                            default:
                                style = bean.getCommonStyle();
                                break;
                        }
                        break;
                    case "2"://图片新闻
                        type = "2";
                        if (bean.getImagess().size() == 1) {
                            pOne = bean.getImagess().get(0).getImageUrlString();
                            hasPicture = true;
                        } else if (bean.getImagess().size() == 2) {
                            pOne = bean.getImagess().get(0).getImageUrlString();
                            pTwo = bean.getImagess().get(1).getImageUrlString();
                            hasPicture = true;
                        } else if (bean.getImagess().size() >= 3) {
                            pOne = bean.getImagess().get(0).getImageUrlString();
                            pTwo = bean.getImagess().get(1).getImageUrlString();
                            pThree = bean.getImagess().get(2).getImageUrlString();
                            hasPicture = true;
                        } else {
                            hasPicture = false;
                        }
                        break;
                    case "3"://直播
                        type = "9";
                        break;
                    case "4"://标题新闻
                        type = "8";
                        break;
                    case "5"://视频新闻
                        type = "3";
                        style = bean.getVideoStyle();
                        break;
                    case "8"://互动直播
                        bean.setID(bean.getRoomId());//将roomId设置成通用Id
                        type = "5";
                        break;
                    case "101"://抓取新闻
                        type = "10";
                        break;
                    default:
                        break;
                }
                ItemSkipUtils.getInstance().itemSkip(new GlobalNewsBean(bean.getTitle(), bean.getID(), bean.getRedirectURL(), bean.getLogo(), style, bean.getPublishDate(), type, Integer.parseInt(bean.getHitCount()),
                        bean.getSource(), bean.getCatalogID(), hasPicture, pOne, pTwo, pThree, bean.getLivetype()), getActivity());
            }
        });


    }

    /**
     * 设置新闻数据
     *
     * @param beanList
     * @param isMore
     */
    @Override
    public void setDatas(List<NewsBean> beanList, boolean isMore) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~~");
        if (isMore) { //加载更多
            setNews(beanList);
        } else {  //刷新
            bannerList.clear();
            if (topNum != 0) { //有置顶
                //显示轮播
                mNewsListBanner.setVisibility(View.VISIBLE);
                if (topNum >= beanList.size()) { //只有置顶
                    bannerList.addAll(beanList);
                    setBanner();
                    newsList = new ArrayList<>();
                } else {//不仅有置顶
                    for (int i = 0; i < topNum; i++) {
                        bannerList.add(beanList.get(i));
                    }
                    beanList.removeAll(bannerList);
                    newsList.clear();
                    newsList.addAll(beanList);
                    setBanner();
                }
            } else {//没有置顶
                //隐藏轮播
                mNewsListBanner.setVisibility(View.GONE);
                newsList.clear();
                newsList.addAll(beanList);
            }
            mDataList.clear();
            setNews(newsList);
        }
    }

    /**
     * 设置新闻
     */
    private void setNews(List<NewsBean> beanList) {
        indexList = new ArrayList<>();
        List<NewsStyleBean> styleBeanList = CommonUtils.getInstance().getGlobalStyleBeanList();
        boolean globalStyle = CommonUtils.getInstance().getGlobalStyle();
        int globalCommon = Integer.parseInt(CommonUtils.getInstance().getGlobalCommon());
        int globalVideo = Integer.parseInt(CommonUtils.getInstance().getGlobalVideo());
        //改变样式
        for (int i = 0; i < beanList.size(); i++) {
            if (isSpecial(i)) {
                if (globalStyle) {
                    position++;
                }
                beanList.get(i).setCommonStyle(styleBeanList.get(position).getCommonStyle());
                beanList.get(i).setVideoStyle(styleBeanList.get(position).getVideoStyle());
            } else {
                beanList.get(i).setCommonStyle(globalCommon);
                beanList.get(i).setVideoStyle(globalVideo);
            }
        }

        if (Integer.parseInt(sectionId) >= 1000000) {
            page++;
        } else {
            try {
                newsId = beanList.get(beanList.size() - 1).getID();
            } catch (Exception e) {
                newsId = "-1";
            }
        }

        //数据类型转换
        for (int i = 0; i < beanList.size(); i++) {
            NewsBean bean = beanList.get(i);
            hasPicture = StringUtils.isPictureSuccess(bean.getLogo());
            String type = "", pOne = "", pTwo = "", pThree = "";
            int style = 0;
            switch (bean.getType()) {
                case "1"://普通新闻
                    type = "1";
                    switch (bean.getPluralPicsFlag()) {
                        case 0:
                            style = bean.getCommonStyle();
                            break;
                        case 1://三图
                            hasPicture = true;
                            if (bean.getImagess().size() == 1) {
                                hasPicture = true;
                                pOne = bean.getImagess().get(0).getImageUrlString();
                            } else if (bean.getImagess().size() == 2) {
                                pOne = bean.getImagess().get(0).getImageUrlString();
                                pTwo = bean.getImagess().get(1).getImageUrlString();
                                hasPicture = true;
                            } else if (bean.getImagess().size() >= 3) {
                                pOne = bean.getImagess().get(0).getImageUrlString();
                                pTwo = bean.getImagess().get(1).getImageUrlString();
                                pThree = bean.getImagess().get(2).getImageUrlString();
                                hasPicture = true;
                            } else {
                                hasPicture = false;
                            }
                            style = 4;
                            break;
                        case 2:
                            style = 3;
                            break;
                        default:
                            style = bean.getCommonStyle();
                            break;
                    }
                    break;
                case "2"://图片新闻
                    type = "2";
                    if (bean.getImagess().size() == 1) {
                        pOne = bean.getImagess().get(0).getImageUrlString();
                        hasPicture = true;
                    } else if (bean.getImagess().size() == 2) {
                        pOne = bean.getImagess().get(0).getImageUrlString();
                        pTwo = bean.getImagess().get(1).getImageUrlString();
                        hasPicture = true;
                    } else if (bean.getImagess().size() >= 3) {
                        pOne = bean.getImagess().get(0).getImageUrlString();
                        pTwo = bean.getImagess().get(1).getImageUrlString();
                        pThree = bean.getImagess().get(2).getImageUrlString();
                        hasPicture = true;
                    } else {
                        hasPicture = false;
                    }
                    break;
                case "3"://直播
                    type = "9";
                    break;
                case "4"://标题新闻
                    type = "8";
                    break;
                case "5"://视频新闻
                    type = "3";
                    style = bean.getVideoStyle();
                    break;
                case "8"://互动直播
                    bean.setID(bean.getRoomId());//将roomId设置成通用Id
                    type = "5";
                    break;
                case "101"://抓取新闻
                    type = "10";
                    break;
                default:
                    break;
            }

            indexList.add(new GlobalNewsBean(bean.getTitle(), bean.getID(), bean.getRedirectURL(), bean.getLogo(), style, bean.getPublishDate(), type, Integer.parseInt(bean.getHitCount()), bean.getSource(), bean.getCatalogID(), hasPicture, pOne, pTwo, pThree, bean.getLivetype(), bean.getCommentCount()));
            //插入广告,每4条新闻后插入一条
            if ((i + 1) % 4 == 0) {
                if (advPostion < advList.size()) {
                    List<AdvertiseBean> list = advList.get(advPostion).getAdvertise();//获取指定广告位内的所有广告
                    advPostion++;
                    if (list != null && list.size() > 0) {
                        int rdNum = new Random().nextInt(list.size());//随机获取一个广告
                        AdvertiseBean advertiseBean = list.get(rdNum);
                        indexList.add(new GlobalNewsBean(advertiseBean.getTitle(), advertiseBean.getId() + "", advertiseBean.getLink(), advertiseBean.getImageUrl(), Integer.parseInt(advertiseBean.getStyle()), advertiseBean.getCatalogID(), "4", 0, advertiseBean.getContentType(), advertiseBean.getLinkId(), true, advertiseBean.getLinkType()));
                    }
                }
            }
        }

//        AppContext app = AppContext.getApp();

        String advSwitch = CommonUtils.getInstance().getListAdv();//加入广告
        if (isFragmentShow && advSwitch.equals("1") && indexList.size() >= 5 && MPermissionUtils.checkPermissions(getActivity(), Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//            QuadNativeAdLoader adLoader = QUAD.getNativeAdLoader(getActivity(), ChannelConfig.PID,
//                    new QuadNativeAdLoadListener() {
//                        @Override
//                        public void onAdLoadSuccess(QuadNativeAd quadNativeAd) {
//
//
//                            JSONObject json = quadNativeAd.getContent();
//                            try {
//                                indexList.add(5, new GlobalNewsBean(json.optString("title"), json.optJSONArray("contentimg").get(0).toString(), "101", json.optInt("action_type", 0), true, quadNativeAd));
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
//                            mDataList.addAll(indexList);
//                            mHeaderAndFooterWrapper.notifyDataSetChanged();
//                        }
//
//                        @Override
//                        public void onAdLoadFailed(int i, String s) {
//                            Log.e("error_adv", s);
//                            mDataList.addAll(indexList);
//                            mHeaderAndFooterWrapper.notifyDataSetChanged();
//                        }
//                    });
//            if (adLoader != null) {
//                adLoader.loadAds();
//            }
        } else {
            mDataList.addAll(indexList);
            mHeaderAndFooterWrapper.notifyDataSetChanged();
        }


    }

    /**
     * 设置轮播
     */
    private void setBanner() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mNewsListBanner.getLayoutParams();
        params.height = ScreenUtils.getScreenWidth(getActivity()) / 2 * 1;
        mNewsListBanner.setLayoutParams(params);
        //配置轮播
        List<String> titleNames = new ArrayList<>();
        for (int i = 0; i < bannerList.size(); i++) {
            titleNames.add(bannerList.get(i).getTitle());
        }
        String[] titleStrings = new String[titleNames.size()];
        titleNames.toArray(titleStrings);
        //当只有一张轮播时
        if (bannerList.size() == 1) {
            mNewsListBanner.setCanLoop(false);
            mNewsListBanner.setTextBanner(titleStrings[0]);
        } else {
            mNewsListBanner.setCanLoop(true);
        }

        //初始化轮播信息
        mNewsListBanner.setPages(new SimpleHolderCreator() {
            @Override
            public Object createHolder() {
                return new BannerViewHolder();
            }
        }, bannerList).startTurning(3000)
                .setPageIndicator(new int[]{R.drawable.news_banner_indicator_off, R.drawable.news_banner_indicator_on}, titleStrings)
                .setPageIndicatorAlign(SimpleBannerView.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
    }

    /**
     * 判断当前位置是否是特殊位置
     *
     * @return
     */
    private boolean isSpecial(int index) {
        position = 0;
        try {
            for (int i = 0; i < pos.length; i++) {
                if (pos[i] == (index + 1)) {
                    position = i;
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 设置置顶数
     *
     * @param num
     */
    @Override
    public void setHeaderNum(int num) {
        int sectionBannerNum = Integer.parseInt(CommonUtils.getInstance().getSectionBannerNum());
        if (num <= sectionBannerNum) {
            topNum = num;
        } else {
            topNum = sectionBannerNum;
        }
        mPresenter.getAdvData(sectionId);
    }

    /**
     * 置顶数获取失败
     */
    @Override
    public void setHeaderError() {
        topNum = 0;
        mPresenter.getAdvData(sectionId);
    }

    /**
     * 设置广告
     *
     * @param beanList
     */
    @Override
    public void setAdvData(List<AdvHomeBean> beanList) {
        advList.clear();
        advList.addAll(beanList);
        if (Integer.parseInt(sectionId) >= 1000000) {
            mPresenter.getDatas(sectionId, page + "");
        } else {
            mPresenter.getDatas(sectionId, newsId);
        }

    }

    /**
     * 广告获取失败
     *
     * @param msg
     */
    @Override
    public void setAdvError(String msg) {
        if (Integer.parseInt(sectionId) >= 1000000) {
            mPresenter.getDatas(sectionId, page + "");
        } else {
            mPresenter.getDatas(sectionId, newsId);
        }
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setEmptyText(msg);
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无网络
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * toast
     *
     * @param msg
     */
    @Override
    public void showMessage(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        Toasty.normal(getActivity(), msg, Toast.LENGTH_SHORT).show();
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    public void showLog(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 轮播holder
     */
    class BannerViewHolder implements SimpleHolder<NewsBean> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, NewsBean data) {
            Glide.with(context.getApplicationContext())
                    .load(data.getLogo())
                    .apply(options)
                    .into(imageView);
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isFragmentShow = !hidden;
    }



    @Override
    public void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("新闻列表");
        MobclickAgent.onResume(getActivity());
    }


    @Override
    public void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("新闻列表");
        MobclickAgent.onPause(getActivity());
    }
}

